Intermediate Tutorials for Machine Learning by Mwiti Derrick

Intermediate Tutorials for Machine Learning by Mwiti Derrick

Author:Mwiti, Derrick [Mwiti, Derrick]
Language: eng
Format: epub
Published: 2020-08-18T16:00:00+00:00


We then compile and fit the model.

model_layer_prunning.compile(optimizer=’adam’,

loss=tf.keras.losses.mean_squared_error,

metrics=[‘mae’, ‘mse’])

model_layer_prunning.fit(X_train,y_train,epochs=300,validation_split=0.1,callbacks=callbacks,verbose=0)

Now, let’s check the mean squared error.

layer_prune_predictions = model_layer_prunning.predict(X_test)

print(‘Layer Prunned MSE %.4f’ % mean_squared_error(y_test,layer_prune_predictions.reshape(3300,)))

Layer Prunned MSE 0.1388

We can’t compare the MSE obtained here with the previous one since we’ve used different pruning parameters. If you’d like to compare them, then ensure that the pruning parameters are similar. Upon testing, layer_pruning_params gave a lower error than the pruning_params for this specific case. Comparing the MSE obtained from different pruning parameters is useful so that you can settle for the one that doesn’t make the model’s performance worse.

Comparing Model Sizes

Let’s now compare the sizes of the models with and without pruning. We start by training and saving the model weights for later use.

def train_save_weights():

model = setup_model()

model.compile(optimizer='adam',

loss=tf.keras.losses.mean_squared_error,

metrics=['mae', 'mse'])

model.fit(X_train,y_train,epochs=300,validation_split=0.2,callbacks=callbacks,verbose=0)

model.save_weights('.models/friedman_model_weights.h5')

train_save_weights()

We’ll set up our base model and load the saved weights. We then prune the entire model. We compile, fit the model, and visualize the results on Tensorboard.

base_model = setup_model()

base_model.load_weights('.models/friedman_model_weights.h5') # optional but recommended for model accuracy

model_for_pruning = tfmot.sparsity.keras.prune_low_magnitude(base_model)

model_for_pruning.compile(

loss=tf.keras.losses.mean_squared_error,

optimizer='adam',

metrics=['mae', 'mse']

)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.